package com.supreme.simpleserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.supreme.commonmybatis.entity.primary.MBStorage;
import com.supreme.commonboot.exception.BizException;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.simpleserver.service.SeataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "seata")
public class SeataController {

    private final SeataService seataService;

    @Autowired
    public SeataController(SeataService seataService) {
        this.seataService = seataService;
    }

    @GetMapping(value = "test")
    public AjaxResp testSeata() {
        return new AjaxResp("testSeata");
    }

    @PostMapping(value = "testZero")
    public AjaxResp testZero(@RequestBody JSONObject bodyData) {
        Integer status = bodyData.getInteger("status");
        if (status == 0) {
            throw new BizException("test zero exception");
        }
        this.seataService.testZero(status);
        return new AjaxResp("testZero");
    }

    @PostMapping(value = "createStorage")
    public MBStorage createStorage(@RequestBody MBStorage storage) {
        return this.seataService.createStorage(storage);
    }
}
